package cl.jpavlich.mpm.model;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.metawidget.inspector.annotation.UiHidden;

import cl.jpavlich.mpm.services.MPMFileUtils;

public class Download extends Command {

	private String fromUrl;
	private File destFile;


	public Download(String fromUrl) {
		this.fromUrl = fromUrl; 
 
	}
	
	@Override
	public void execute() throws Exception  {
		URL url = new URL(fromUrl);
		destFile = new File(MPMFileUtils.getRepositoryDir(), url.getFile());
		FileUtils.copyURLToFile(url, destFile);
	}

	@Override
	public void undo() {
		// do nothing
		
	}

	public String getFromUrl() {
		return fromUrl;
	}

	public void setFromUrl(String fromUrl) {
		this.fromUrl = fromUrl;
	}

	@Override
	@UiHidden
	public String getCommandDescription() {
		return "Download " + fromUrl;
	}
	
	@UiHidden
	public File getDestFile() {
		return destFile;
	}

}
